package csu.web.mypetstore.controller;

import csu.web.mypetstore.domain.*;
import csu.web.mypetstore.pojo.*;
import csu.web.mypetstore.service.*;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class SceneController {
    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService catalogService;

    @Autowired
    private ItemService itemService;

    @Autowired
    private AccountService accountService;
    @Autowired
    private OrdersService orderService;
    @Autowired
    private OrderDetailService orderDetailsService;
    @Autowired
    private CartService cartService;
    @Autowired
    private DeliveryOrderService deliveryService;

    @GetMapping("cartForm")
    public String cartForm(HttpSession session, Model model) {
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        if(loginAccount== null)
        {
            return "account/signon";
        }

        if (session.getAttribute("cart") == null&& loginAccount == null) {
            session.setAttribute("cart", new Cart());
        }
        if (session.getAttribute("cart") == null&& loginAccount!= null) {
            Cart cart = new Cart();

          List<DB_Cart> cartItems =  cartService.getCartItems(loginAccount.getUsername());

          cart.addItemByDBCart(cartItems,itemService);
          session.setAttribute("cart", cart);

        }
        return "cart/cart";
    }
    @GetMapping("categoryForm")
    public String categoryForm(HttpSession session, @RequestParam("categoryId") String categoryId) {


        Category category = catalogService.getCategory(categoryId);
        List<Product> productList = productService.getProductListByCategory(categoryId);
        session.setAttribute("category", category);
        session.setAttribute("productList", productList);
        return "catalog/category";
    }
    @GetMapping("itemForm")
    public String itemForm(HttpSession session,@RequestParam("itemId") String itemId) {
        Item item =itemService.getItem(itemId);
        Product product = item.getProduct();
        System.out.println(product.toString());
        session.setAttribute("item", item);
        session.setAttribute("product", product);
        // 处理用户活动日志
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        String username = (loginAccount != null) ? loginAccount.getUsername() : null;

        return "catalog/item";
    }
    @GetMapping("mainForm")
    public String mainForm(HttpSession session) {
        return "catalog/main";
    }
    @GetMapping("newOrderForm")
    public String newOrderForm(HttpSession session, Model model) {
        Account loginAccount = (Account) session.getAttribute("loginAccount");


        if (loginAccount == null) {
            return "account/signon";
        }
        else {
            //zcy

            String userId = loginAccount.getUsername();
            String action = "Access_New_Order_Form";

            return "order/newOrder";
        }
    }
    @GetMapping("productForm")
    public String productForm(HttpSession session, @RequestParam("productId") String productId) {
        Product product = productService.getProduct(productId);

        List<Item> itemList = itemService.getItemListByProduct(productId);
        session.setAttribute("product", product);
        session.setAttribute("itemList", itemList);
        return "catalog/product";
    }
    @GetMapping("registerForm")
    public String registerForm(HttpSession session) {
        //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        return "account/NewAccountForm";
    }
    @GetMapping("signonForm")
    public String signonForm(HttpSession session) {
        return "account/signon";
    }
    @GetMapping("cmsMainForm")
    public String cmsMainForm(HttpSession session) {

        List<Category> categoryList = catalogService.getAllCategories();
        session.setAttribute("categoryList", categoryList);
        return "cms/catalog/main";
    }
    @GetMapping("cmsCategoryForm")
    public String cmsCategoryForm(HttpSession session, @RequestParam("categoryId") String categoryId, Model model) {
        if(session.getAttribute("loginAccount") == null)
        {
            return "cms/account/signon";
        }
        Category category = catalogService.getCategory(categoryId);
        List<Product> productList = productService.getProductListByCategory(categoryId);
        session.setAttribute("category", category);
        session.setAttribute("productList", productList);
        List<Category> categoryList = catalogService.getAllCategories();
        session.setAttribute("categoryList", categoryList);
        model.addAttribute("products", productList);
        return "cms/catalog/category";
    }
    @GetMapping("cmsProductForm")
    public String cmsProductForm(HttpSession session, @RequestParam("productId") String productId, Model model) {
        Product product = productService.getProduct(productId);
        List<Item> itemList = itemService.getItemListByProduct(productId);
        session.setAttribute("productId", productId);
        session.setAttribute("product", product);
        session.setAttribute("itemList", itemList);
        model.addAttribute("items", itemList);
        return "cms/catalog/product";
    }
    @GetMapping("productAddForm")
    public String productAddForm(HttpSession session,@RequestParam("categoryId") String categoryId) {

        session.setAttribute("categoryId", categoryId);


        return "cms/catalog/add_product";
    }
    @GetMapping("cmsProductUpdateForm")
    public String productUpdateForm(HttpSession session, @RequestParam("productId") String productId, Model model) {

        Product product = productService.getProduct(productId);
         model.addAttribute("product", product);

        return "cms/catalog/update_product";
    }
    @GetMapping("updateItemForm")
    public String updateItemForm(HttpSession session, @RequestParam("workingItemId") String itemId,Model model) {
        Item item = itemService.getItem(itemId);
        item.setQuantity(itemService.getLeftItemNum(itemId));
        model.addAttribute("item", item);

        return "cms/catalog/update_item";
    }
    @GetMapping("addItemForm")
    public String addItemForm(HttpSession session, @RequestParam("productId") String productId) {

        session.setAttribute("productId", productId);


        return "cms/catalog/add_item";
    }
    @GetMapping("cmsOrderForm")
    public String cmsOrderForm(HttpSession session, Model model) {
       if(session.getAttribute("loginAccount") == null)
       {
           return "cms/account/signon";
       }
        List<Orders> orderList = orderService.queryAllOrders();
        session.setAttribute("orderList", orderList);


        return "cms/order/order_manager";
    }
    @GetMapping("orderUpdateForm")
    public String orderUpdateForm(HttpSession session, @RequestParam("orderId") String orderId, Model model) {

        Orders orders = orderService.getOrder(Integer.parseInt(orderId));
        session.setAttribute("order", orders);
        session.setAttribute("orderId", orderId);

        return "cms/order/order_update";
    }
    @GetMapping("orderDetailForm")
    public String orderDetailForm(HttpSession session, @RequestParam("orderId") String orderId, Model model) {

        List<OrderDetail> orderDetailList = orderDetailsService.queryOrderDetail(Integer.parseInt(orderId));
        model.addAttribute("orderDetailList", orderDetailList);
        model.addAttribute("orderId", orderId);

        return "cms/order/order_detail";
    }
    @GetMapping("cmsSignonForm")
    public String cmsSignonForm() {
        return "cms/account/signon";
    }
    @GetMapping("cmsRegisterForm")
    public String cmsRegisterForm() {
        return "cms/account/NewAccountForm";
    }
    @GetMapping("categoryAddForm")
    public String categoryAddForm() {
        return "cms/catalog/add_category";
    }
    @GetMapping("categoryUpdateForm")
    public String categoryUpdateForm(HttpSession session, @RequestParam("categoryId") String categoryId, Model model) {

        Category category = catalogService.getCategory(categoryId);
        model.addAttribute("category", category);
        //session.setAttribute("categoryId", categoryId);
        return "cms/catalog/update_category";
    }
    @GetMapping("orderDeliveryForm")
    public String orderDeliveryForm(Model model) {

        List<DeliveryOrder> deliveryOrderList = deliveryService.getAllOrders();


        model.addAttribute("orderDeliveryList", deliveryOrderList);

        return "cms/order/order_delivery";

    }
    @GetMapping("myPackageForm")
    public String myPackageForm(HttpSession session, Model model) {
        Account loginAccount = (Account) session.getAttribute("loginAccount");

        List<DeliveryOrder>deliveryOrderList = deliveryService.getOrderByUserId(loginAccount.getUsername());

        for(DeliveryOrder deliveryOrder:deliveryOrderList)
        {

            List<OrderDetail> orderDetailList = orderDetailsService.queryOrderDetail(deliveryOrder.getOrderid());

            deliveryOrder.setOrderDetails(orderDetailList);


        }

        model.addAttribute("deliveryOrderList", deliveryOrderList);



        return "order/myPackage";
    }


}
